package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionType;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;

import java.lang.reflect.Array;
import java.security.Key;
import java.util.*;

public class QuestionBankServiceScwImpl extends QuestionBankServiceImpl implements QuestionBankService {

    @Override
    public List<Question> importQuestions(String questionTextFile) {
        String fileContent = getQuestionFileContent();
        List<Question> questionsList = new ArrayList<>();
        String description;
        int no = 0;
        String[] lines = fileContent.split("\n");
        for (int i = 0; i < lines.length; i = i + 6) {
            String line = lines[i];
            no = Integer.parseInt(lines[i].substring(0, lines[i].indexOf('、')));
            description = lines[i].substring(lines[i].indexOf('、') + 1);
            Question q = new Question(no, description, QuestionType.SINGLE_CHOICE);
            q.add(new Option(lines[i + 1].substring(lines[i + 1].indexOf('、') + 1), 'A', false));
            q.add(new Option(lines[i + 2].substring(lines[i + 2].indexOf('、') + 1), 'B', false));
            q.add(new Option(lines[i + 3].substring(lines[i + 3].indexOf('、') + 1), 'C', false));
            q.add(new Option(lines[i + 4].substring(lines[i + 4].indexOf('、') + 1), 'D', false));
            questionsList.add(q);
        }
        return questionsList;
    }


    @Override
    public List<Question> importCorrectAnswers(String correctAnswerTextFile) {
        List<Question> questionList = importQuestions("");
        Map<Integer, Character> answer = readCorrectAnswers2("");
        for (Question q : questionList) {
            if (!answer.containsKey(q.getNo())) continue;
            char choice = answer.get(q.getNo());
            Option option = q.getOptions().stream()
                    .filter(ch -> ch.getChoice() == choice)
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("找不到选项"));
            option.changeToCorrectAnswer();
        }
        return questionList;
    }

    @Override
    public List<AnswerModel> readCorrectAnswers(String correctAnswerTextFile) {

        String fileContent = getAnswerFileContent();
        List<AnswerModel> answerModels = new ArrayList<>();
        char correctAnswer;
        int no;
        String[] lines = fileContent.split("\n");

        for (int i = 0; i < lines.length; i += 2) {
            String line = lines[i];
            for (int m = 0; m < lines[i].length(); m++) {
                String[] charNo = lines[i].split("\t");
                no = Integer.parseInt(charNo[m]);
                String[] charAnswer = lines[i + 1].split("\t");
                correctAnswer = charAnswer[m].charAt(0);
                AnswerModel answer = new AnswerModel(no, correctAnswer);
                answerModels.add(answer);
            }
        }
        return answerModels;
    }

    public Map<Integer, Character> readCorrectAnswer2(String correctAnswerTextFile) {
        Map<Integer, Character> result = new HashMap<>();
        String[] lines = getAnswerFileContent().split("\n");
        for (int i = 0; i < lines.length; i += 2) {
            for (int j = 0; j < lines[i].length(); j++) {
                String[] nos = lines[i].split("\t");
                String[] answers = lines[i + 1].split("\t");
                String no = nos[j];
                String answer = answers[j];
                result.put(Integer.parseInt(no), answer.charAt(0));
            }
        }
        return result;
    }

    @Override
    public List<Question> importCategories(String categoryExcelFile) {
        return null;
    }
}
